package com.huika.dealontop.control.myinfo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.support.domain.MyCouponBean;
import com.huika.dealontop.utils.DateTimeTool;

import java.text.DecimalFormat;

/**
 * auther: chenyuan.
 * time: 2015/11/17 14:41
 * description:我的优惠券适配器
 */
public class MyCouponAdapter extends BaseAda<MyCouponBean> {
    public MyCouponAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_my_coupon, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        MyCouponBean bean = getItem(position);
        vh.status_nouse_tv.setVisibility(bean.discountStatus.equals("1") ? View.VISIBLE : View.GONE);
        vh.status_expired_loy.setVisibility(bean.discountStatus.equals("3") ? View.VISIBLE : View.GONE);
        vh.status_beenuse_loy.setVisibility(bean.discountStatus.equals("2") ? View.VISIBLE : View.GONE);

        vh.common_time_tv.setText(String.format(mContext.getString(R.string.my_coupon_expired_des),DateTimeTool.dmyDateParse(bean.expiredDate)));
        if (bean.discountStatus.equals("1")) {
            vh.general_item_bg.setBackgroundResource(R.mipmap.my_coupon_bg_green);
            vh.common_time_tv.setTextColor(Color.parseColor("#169477"));
        } else if (bean.discountStatus.equals("3")) {
            vh.general_item_bg.setBackgroundResource(R.mipmap.my_coupon_bg_gray);
            vh.common_time_tv.setTextColor(Color.parseColor("#939292"));
        } else {
            vh.general_item_bg.setBackgroundResource(R.mipmap.my_coupon_bg_pink);
            vh.common_time_tv.setTextColor(Color.parseColor("#d34439"));
        }
        vh.status_expired_time_tv.setText(DateTimeTool.dmyDateParse(bean.expiredDate));
        vh.status_beenuse_time_tv.setText(DateTimeTool.dmyDateParse(bean.useDate));

        if (!TextUtils.isEmpty(bean.vouchersAmount)) {
            String arr[] = bean.vouchersAmount.split(".");
            DecimalFormat format=new DecimalFormat("#");
            vh.coupon_num_tv.setText(format.format(Double.parseDouble(bean.vouchersAmount)));
        }

        return convertView;
    }

    class ViewHolder {

        public TextView status_nouse_tv;
        public TextView coupon_num_tv;
        public TextView common_time_tv;
        public TextView status_expired_time_tv;//已过期过期时间
        public TextView status_beenuse_time_tv;//已使用使用时间
        public RelativeLayout status_expired_loy;
        public RelativeLayout status_beenuse_loy;
        public RelativeLayout general_item_bg;

        public ViewHolder(View convertView) {
            status_nouse_tv = (TextView) convertView.findViewById(R.id.status_nouse_tv);
            common_time_tv = (TextView) convertView.findViewById(R.id.common_time_tv);
            coupon_num_tv = (TextView) convertView.findViewById(R.id.coupon_num_tv);
            status_expired_time_tv = (TextView) convertView.findViewById(R.id.status_expired_time_tv);
            status_beenuse_time_tv = (TextView) convertView.findViewById(R.id.status_beenuse_time_tv);
            status_expired_loy = (RelativeLayout) convertView.findViewById(R.id.status_expired_loy);
            status_beenuse_loy = (RelativeLayout) convertView.findViewById(R.id.status_beenuse_loy);
            general_item_bg = (RelativeLayout) convertView.findViewById(R.id.general_item_bg);
        }
    }


}
